package com.example.leetdemo.process;

public class Solution498 {

    public int[] findDiagonalOrder(int[][] mat) {
        int m = mat.length, n = mat[0].length;
        int line = m + n - 1;
        int[] res = new int[m * n];
        int i = 0, j = 0;
        int flag = 0;
        for (int k = 0; k < m * n; k++) {
            res[k] = mat[i][j];
            if (flag % 2 == 0) {
                if (i == 0) {
                    j++;
                    flag++;
                } else if (j == n - 1) {
                    i--;
                    flag++;
                } else {
                    i--; j++;
                }
            } else {
                if (j == 0) {
                    i++;
                    flag++;
                } else if (i == m - 1) {
                    j++;
                    flag++;
                } else {
                    i++; j--;
                }
            }
        }
        return res;
    }

}
